/**
 * @project readnote
 * @Title BookServiceImpl.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.user.service.impl;

import com.cr.backbone.hibenate.AppBaseServiceImpl;
import com.cr.backbone.util.UtilsTranscoding;
import com.readnote.user.dao.UserDAO;
import com.readnote.user.dao.UserInfoDAO;
import com.readnote.user.model.User;
import com.readnote.user.model.UserInfo;
import com.readnote.user.service.UserService;



public class UserServiceImpl extends AppBaseServiceImpl<User> implements UserService {
	
	public UserDAO getDAO(){
		return ((UserDAO)(dao));
	}

	private UserInfoDAO userInfoDAO;
	
	
	/**
	 * 根据用户名、密码查询用户
	 * 密码需要加密后再作为查询条件执行查询
	 * @param name
	 * @param _password
	 * @return
	 * @author huxh
	 * @date 2012-02-14
	 */
	public User getUserByLoginNameAndPassword(String name, String password){
		User user = getUserByLoginName(name);
		if(user == null || user.isNullRow() )
			return null;
		
		if(!UtilsTranscoding.Md5PassWordFormat(password).equals(user.getPassword())){
			return null;
		}
		return user;
	}

	
	/**
	 * 根据用户名查询用户
	 * @param name
	 * @return
	 * @author huxh
	 * @date 2012-02-14
	 */
	public User getUserByLoginName(String name){
		return getDAO().getUserByLoginName(name);
	}

	
	/**
	 * 注册新用户
	 * @param user
	 * @return
	 * @author huxh
	 * @date 2012-02-14
	 */
	public void saveRegisterUser(User user){
		String password=UtilsTranscoding.Md5PassWordFormat(user.getPassword());
		user.setPassword(password);
		Long id = getDAO().save(user);
		
		User _user=getDAO().get(id);
		UserInfo userInfo=new UserInfo();
		userInfo.setUser(_user);
		userInfoDAO.save(userInfo);
	}

	public UserInfoDAO getUserInfoDAO() {
		return userInfoDAO;
	}

	public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
		this.userInfoDAO = userInfoDAO;
	}
	
}
